package weifei.live.simulation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import weifei.live.simulation.entity.*;
import weifei.live.simulation.service.GroupService;
import weifei.live.simulation.service.UserService;
import weifei.web.Result;


@RestController
@RequestMapping("/group")
public class GroupController
{
    @Autowired
    private UserService userService;
    @Autowired
    private GroupService groupService;

    @Autowired
    private Sort dao_sort;
    @Autowired
    private Avatar dao_avatar;
    @Autowired
    private ChatGroup dao_group;
    @Autowired
    private GroupSort dao_group_sort;
    @Autowired
    private GroupMember dao_group_member;




    @GetMapping("/getGroupByID")
    @PostMapping("/getGroupByID")
    public Result getGroupByID(@RequestParam("id") Integer groupId)
    {
        if (groupId == null)
            return Result.exception("参数错误");

        var g = this.groupService.getGroupByID(groupId);
        if (g == null)
            return Result.failed("群聊不存在");
        return Result.success("获取成功", g);
    }


    @GetMapping("/getGroupListByOwnerID")
    @PostMapping("/getGroupListByOwnerID")
    public Result getGroupListByOwnerID(@RequestParam("id") Integer ownerId)
    {
        if (ownerId == null)
            return Result.exception("参数错误");
        QueryWrapper<ChatGroup> qw_group = new QueryWrapper<>();
        qw_group.eq(ChatGroup.Column.OwnerID.getName(), ownerId);
        var list = this.dao_group.selectList(qw_group);
        return Result.success("获取成功", list);
    }


   @GetMapping("/searchGroupByName")
   @PostMapping("/searchGroupByName")
   public Result searchGroupByName(@RequestParam("name") String searchName)
   {
       if (searchName == null)
           return Result.exception("参数错误");

       QueryWrapper<ChatGroup> qw_group = new QueryWrapper<>();
       qw_group.like(ChatGroup.Column.Name.getName(), searchName);
       var list = this.dao_group.selectList(qw_group);
       return Result.success("搜索成功", list);
   }


    @GetMapping("/getGroupIDsBySortID")
    @PostMapping("/getGroupIDsBySortID")
    private Result getGroupIDsBySortID(@RequestParam("id") Integer sortId)
    {
        if (sortId == null)
            return Result.exception("参数错误");

        QueryWrapper<GroupSort> qw_group_sort = new QueryWrapper<>();
        qw_group_sort.eq(GroupSort.Column.SortID.getName(), sortId);
        var gs_list = this.dao_group_sort.selectList(qw_group_sort);
        var ids = new Integer[gs_list.size()];
        for (var i = 0; i < gs_list.size(); i++)
        {
            ids[i] = gs_list.get(i).getGroupId();
        }
        return Result.success("获取成功", ids);
    }



    @GetMapping("/getGroupAvatarContentByGroupID")
    @PostMapping("/getGroupAvatarContentByGroupID")
    public Result getGroupAvatarContentByGroupID(@RequestParam("id") Integer groupId)
    {
        if (groupId == null)
            return Result.exception("参数错误");

        var g = this.groupService.getGroupByID(groupId);
        if (g == null)
            return Result.failed("群聊不存在");

        QueryWrapper<Avatar> qw_avatar = new QueryWrapper<>();
        qw_avatar.eq(Avatar.Column.ID.getName(), g.getAvatarId());
        var a = this.dao_avatar.selectOne(qw_avatar);
        if (a == null)
            return Result.failed("头像不存在");
        return Result.success("获取成功", a.getContent());
    }


   @GetMapping("/getSorts")
   @PostMapping("/getSorts")
   public Result getSorts(@RequestParam("id") Integer groupId)
   {
       if (groupId == null)
           return Result.exception("参数错误");

       var g = new ChatGroup(groupId).selectById();
       if (g == null)
           return Result.failed("群聊不存在");

       QueryWrapper<GroupSort> qw_group_sort = new QueryWrapper<>();
       qw_group_sort.eq(GroupSort.Column.GroupID.getName(), groupId);

       QueryWrapper<Sort> qw_sort = new QueryWrapper<>();
       var gs_list = this.dao_group_sort.selectList(qw_group_sort);
       var sorts = new Sort[gs_list.size()];
       for (var i = 0; i < gs_list.size(); i++)
       {
           qw_sort.clear();
           qw_sort.eq(Sort.Column.ID.getName(), gs_list.get(i).getSortId());
           sorts[i] = this.dao_sort.selectOne(qw_sort);
       }
       return Result.success("获取成功", sorts);
   }


    // 此处有 登录拦截
    @PostMapping("/disband")
    public Result disbandGroup(HttpServletRequest request, @RequestParam("id") Integer groupId)
    {
        if (groupId == null)
            return Result.exception("参数错误");

        var token = request.getHeader("token");
        var user_id = this.userService.getUserIDByToken(token);

        ChatGroup target = this.groupService.getGroupByID(groupId);
        if (target == null)
            return Result.failed("群聊不存在");
        if (!target.getOwnerId().equals(user_id))
            return Result.failed("没有权限");

        if (!target.deleteById())
            return Result.exception("解散群聊失败，数据库出错");

        QueryWrapper<GroupMember> qw_group_member = new QueryWrapper<>();
        qw_group_member.clear();
        qw_group_member.eq(GroupMember.Column.GroupID.getName(), groupId);
        if (!this.dao_group_member.delete(qw_group_member))
            return Result.exception("解散失败，数据库出错");

        return Result.success("解散成功");
    }


    @PostMapping("/addGroupToTradeArea")
    public Result addGroupToTradeArea(HttpServletRequest request, @Valid @RequestBody TradeAreaGroup taGroup)
    {
        var ta_id = taGroup.getTradeAreaId();
        var g_id = taGroup.getGroupId();

        var token = request.getHeader("token");
        var user_id = this.userService.getUserIDByToken(token);

        ChatGroup target_group = this.groupService.getGroupByID(g_id);
        if (target_group == null)
            return Result.failed("群聊不存在");
        if (!target_group.getOwnerId().equals(user_id))
            return Result.failed("没有权限");

        TradeArea target_tradeArea = new TradeArea(ta_id).selectById();
        if (target_tradeArea == null)
            return Result.failed("商圈不存在");

        if (!taGroup.insert())
            return Result.exception("将群聊添加到商圈失败，数据库出错");
        return Result.success("将群聊添加到商圈成功");
    }


    @PostMapping("/addSort")
    public Result addSort(
            HttpServletRequest request, @Valid @RequestBody GroupSort groupSort)
    {
        var groupId = groupSort.getGroupId();
        var sortId = groupSort.getSortId();

        var token = request.getHeader("token");
        var user_id = this.userService.getUserIDByToken(token);

        ChatGroup target = this.groupService.getGroupByID(groupId);
        if (target == null)
            return Result.failed("群聊不存在");
        if (!target.getOwnerId().equals(user_id))
            return Result.failed("没有权限");

        QueryWrapper<Sort> qw_sort = new QueryWrapper<>();
        qw_sort.eq(Sort.Column.ID.getName(), sortId);
        var sort = this.dao_sort.selectOne(qw_sort);
        if (sort == null)
            return Result.failed("分类不存在");

        if (!groupSort.insert())
            return Result.exception("添加分类失败，数据库出错");
        return Result.success("添加分类成功");
    }


    @PostMapping("/deleteSort")
    public Result deleteSort(
            HttpServletRequest request, @Valid @RequestBody GroupSort groupSort)
    {
        var groupId = groupSort.getGroupId();
        var sortId = groupSort.getSortId();

        var token = request.getHeader("token");
        var user_id = this.userService.getUserIDByToken(token);

        ChatGroup target_group = this.groupService.getGroupByID(groupId);
        if (target_group == null)
            return Result.failed("群聊不存在");
        if (!target_group.getOwnerId().equals(user_id))
            return Result.failed("没有权限");

        QueryWrapper<GroupSort> qw_group_sort = new QueryWrapper<>();
        qw_group_sort.eq(GroupSort.Column.GroupID.getName(), groupId);
        qw_group_sort.eq(GroupSort.Column.SortID.getName(), sortId);
        var target_gs = this.dao_group_sort.selectOne(qw_group_sort);
        if (target_gs == null)
            return Result.failed("群聊 " + target_group.getName() + "不属于该分类");

        if (!target_gs.deleteById())
            return Result.exception("删除分类失败，数据库出错");
        return Result.success("添加分类成功");
    }

}








